package com.kg.fiber.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import com.kg.fiber.FiberviewApplication;
import com.kg.fiber.service.Warnings;

public class WarningPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2099199737133442482L;
	private JLabel lblTime;
	private JLabel lblMark;
	private String host;

	/**
	 * Create the panel.
	 */
	public WarningPanel() {
		setBorder(new CompoundBorder(new EmptyBorder(2, 5, 2, 5), new MatteBorder(0, 0, 1, 0, (Color) new Color(192, 192, 192))));
		setOpaque(false);
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setOpaque(false);
		add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{8, 240, 55, 0, 0};
		gbl_panel.rowHeights = new int[]{18, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		lblTime = new JLabel("2017-10-10 08:53:24");
		lblTime.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		GridBagConstraints gbc_lblTime = new GridBagConstraints();
		gbc_lblTime.anchor = GridBagConstraints.WEST;
		gbc_lblTime.insets = new Insets(0, 0, 0, 5);
		gbc_lblTime.gridx = 1;
		gbc_lblTime.gridy = 0;
		panel.add(lblTime, gbc_lblTime);
		
		lblMark = new JLabel("我也不知道哪里报警了");
		lblMark.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		GridBagConstraints gbc_lblMark = new GridBagConstraints();
		gbc_lblMark.insets = new Insets(0, 0, 0, 5);
		gbc_lblMark.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblMark.gridx = 2;
		gbc_lblMark.gridy = 0;
		panel.add(lblMark, gbc_lblMark);
		
		btnNewButton = new JButton("确 认");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				confirm();
			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.gridx = 3;
		gbc_btnNewButton.gridy = 0;
		panel.add(btnNewButton, gbc_btnNewButton);
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btnNewButton.setPreferredSize(new Dimension(100, 36));

	}
	
	public void setWarning(Warnings.Item item) {
		this.lblTime.setText(sdf.format(item.getTime()));
		this.lblMark.setText(item.getMark());
		this.host = item.getHost();
	}
	
	private void confirm() {
		
		Warnings warnings = FiberviewApplication.getSpring().getBean(Warnings.class);
		warnings.confirm(host);
		
	}
	
	public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private JButton btnNewButton;

}
